import { _decorator, BoxCollider, CCInteger, Component, director, EventKeyboard, EventTouch, ICollisionEvent, input, Input, ITriggerEvent, KeyCode, Node, RigidBody, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('PlayerController')
export class PlayerController extends Component {
    @property(Node)
    camera:Node;
    @property(Node)
    player:Node;

    @property(Vec3)
    cameraOffset:Vec3 = new Vec3();

    @property(CCInteger)
    forwardForce:number = 0; //前进推力

    @property(CCInteger)
    sideForce:number = 0; //左右推力

    rigidBody:RigidBody; //钢体
    collider:BoxCollider; //碰撞体
    isLeftDown:boolean = false;
    isRightDown:boolean = false;
    // 游戏状态
    isPlaying:boolean = false;

    start() {
        this.rigidBody = this.player.getComponent(RigidBody);
        this.collider = this.player.getComponent(BoxCollider);
        // 键盘事件
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        // 碰撞事件
        if (this.collider) {
            this.collider.on('onCollisionEnter', this.onCollisionEnter, this);
            this.collider.on('onTriggerEnter', this.onTriggerEnter, this);
        }
    }
    onDestroy(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
        if (this.collider) {
            this.collider.off('onCollisionEnter', this.onCollisionEnter, this);
            this.collider.off('onTriggerEnter', this.onTriggerEnter, this);
        }
    }

    tmpPos = new Vec3();
    update(deltaTime: number) {
        // 相机跟随
        this.player.getPosition(this.tmpPos);
        this.tmpPos.add(this.cameraOffset);
        this.camera.position = this.tmpPos;
        if (this.isPlaying) {
            // 施加作用力
            let force = new Vec3(0, 0, this.forwardForce * deltaTime);
            if (this.rigidBody) {
                this.rigidBody.applyForce(force);
                if(this.isLeftDown){
                    let leftForce = new Vec3(this.sideForce * deltaTime, 0, 0);
                    this.rigidBody.applyForce(leftForce);
                }
                if(this.isRightDown){
                    let rightForce = new Vec3(-this.sideForce * deltaTime, 0, 0);
                    this.rigidBody.applyForce(rightForce);
                }
            }
            // 掉落检测
            if (this.player.getPosition().y < 0) {
                // 停止游戏
                this.isPlaying = false;
                // 自定义事件，关卡失败
                director.getScene().emit('level_failed');
            }
        }
    }
    // 键盘按下
    onKeyDown(event: EventKeyboard) {
        console.log('onKeyDown');
        if(event.keyCode == KeyCode.KEY_A){
            this.isLeftDown = true;
        }
        if(event.keyCode == KeyCode.KEY_D){
            this.isRightDown = true;
        }
    }
    // 键盘抬起
    onKeyUp(event: EventKeyboard) {
        console.log('onKeyUp');
        if(event.keyCode == KeyCode.KEY_A){
            this.isLeftDown = false;
        }
        if(event.keyCode == KeyCode.KEY_D){
            this.isRightDown = false;
        }
    }

    // 碰撞事件
    onCollisionEnter(event:ICollisionEvent){
        console.log('hit');
        // 碰到障碍物
        if(event.otherCollider.node.name == 'Obstacle'){
            // 停止游戏
            this.isPlaying = false;
            // 自定义事件，关卡失败
            director.getScene().emit('level_failed');
        }
    }

    // 触发事件
    onTriggerEnter(event:ITriggerEvent){
        // 通关
        console.log('Run The End!')
        // 停止游戏
        this.isPlaying = false;
        // 自定义事件，关卡成功
        director.getScene().emit('level_successful');
    }
}


